<?php

    $tpl = new SimpleTemplate(VIEW.'aluno/alterarAluno.html');

    $func = new functions;       
    $dados = new aluno;
    $sel = new select;
    
    if(empty($_SESSION['idAluno'])){
        $func->go("home.php?pg=buscarAluno");
    }
    else{
        $id = $_SESSION['idAluno'];
        $_SESSION['idAluno'] = $id;                
    }
    
    $aluno = $dados->select_unique("SELECT * FROM aluno WHERE id=$id");

    if(!isset($id) || !empty($id)){
   
        $tpl->MATRICULA = $aluno["matricula"];
        $tpl->NOME = $aluno["nome"];
        $tpl->CPF = $func->get_value($aluno["cpf"])->format("cpf")->get();
        $tpl->IDENTIDADE = $aluno["identidade"];
        $tpl->TELEFONE = $func->get_value($aluno["telefone"])->format("phone")->get();
        $tpl->CELULAR = $func->get_value($aluno["celular"])->format("phone")->get();
        $tpl->EMAIL = $aluno["email"];
        $tpl->DTNASCIMENTO = $func->get_value($aluno["dtNascimento"])->to_php_date("d/m/Y")->get();
        $tpl->RUA = $aluno["rua"];
        $tpl->COMPLEMENTO = $aluno["complemento"];
        $tpl->BAIRRO = $aluno["bairro"];
        $tpl->NUMERO = $aluno["numero"];
        $tpl->CIDADE = $aluno["cidade"];
        $tpl->CEP = $func->get_value($aluno["cep"])->format("cep")->get();
        $tpl->MOTIVO = $aluno["motivo"];
        $tpl->OBSERVACOES = $aluno["observacoes"];
        $tpl->FOTO = UPLOAD.$aluno["foto"];
        

        if($aluno["foto"]=="")
           $tpl->FOTO =  IMAGES."sem_imagem.gif";
        
        
        $estados = $sel->uf();
        foreach($estados as $uf){
            $tpl->UF = $uf;

            ($aluno["uf"] == $uf)? $tpl->SELECTED = 'selected' : $tpl->clear("SELECTED");

            $tpl->block("BLOCK_UF");
        }       
        
        $sexo = $sel->sexo();
        foreach ($sexo as $s){
            $tpl->SEXO = $s;
            
            ($aluno["sexo"] == $s)? $tpl->SELECTED = 'selected': $tpl->clear("SELECTED");
            
            $tpl->block("BLOCK_SEXO");
        }
               
       
        
        ($aluno["situacao"] == 'Ativo') ? $tpl->block("BLOCK_CHECKED"): $tpl->block ("BLOCK_UNCHECKED");
        
        if($_SERVER['REQUEST_METHOD']=='POST'){
            if(!empty($_FILES["foto"]["name"])){
                if($_FILES["foto"]["name"]!= $aluno["foto"]){
                    $func->deleteUploaded(UPLOAD.$aluno["foto"]);
                }
            }
            else{
                $_FILES["foto"]["name"] = $aluno["foto"];
                $_FILES["foto"]["type"] = "image/".substr($aluno["foto"],-3);
                
                array_push($_POST, "atualizarIMG");
                $_POST["atualizarIMG"] = $aluno["foto"];
            }
        }
        
        if($_SERVER['REQUEST_METHOD']=='POST'){
            $_POST["senha"] = $aluno["senha"];               
        }
        
        if($_SERVER['REQUEST_METHOD']=='POST'){
            array_push($_POST, "codigo");
            $_POST["codigo"] = $aluno["codigo"];               
        }
        
        $result = $dados->update('aluno', $_POST, $id);
        
        if($result)
            $func->go("home.php?pg=alunoSuccess&action=atu&id=".$aluno['id']);
    }
    

    $tpl->show();

?>
